//
//  ViewController.m
//  14_Work
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define FIRSTREUSE @"reuseFirst"
#import "MyCollectionViewCell.h"
#import "FirstCollectionReusableView.h"
#define REUSEVIEW @"firstReuse"
#import "SecCollectionReusableView.h"
#define SECVIEW @"secReuse"
#import "ThiredCollectionReusableView.h"
#define THIREDVIEW @"thiredView"
#define TOPREUSE @"topReuse"
#import "TopCollectionViewCell.h"

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionView *topCollectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCollectionView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat width = (CGRectGetWidth(self.view.bounds) - 15) / 2;
    layout.itemSize = CGSizeMake(width, width / 2);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:FIRSTREUSE];
    [self.collectionView registerClass:[FirstCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:REUSEVIEW];
    [self.collectionView registerClass:[SecCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SECVIEW];
    [self.collectionView registerClass:[ThiredCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:THIREDVIEW];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(370, 0, 0, 0);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, -370, self.view.bounds.size.width, 340)];
    backView.backgroundColor = [UIColor cyanColor];
    [self.collectionView addSubview:backView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, backView.frame.size.width / 3, 30)];
    label.text = @"栏目";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(backView.frame.size.width - 95, 0, 90, 30);
    [button setTitle:@"查看全部" forState:UIControlStateNormal];
    [backView addSubview:label];
    [backView addSubview:button];
    UICollectionViewFlowLayout *layout1 = [[UICollectionViewFlowLayout alloc] init];
    layout1.itemSize = CGSizeMake(270, 100);
    layout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout1.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.topCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, backView.frame.size.width, 340) collectionViewLayout:layout1];
    self.topCollectionView.backgroundColor = [UIColor whiteColor];
    self.topCollectionView.delegate = self;
    self.topCollectionView.dataSource = self;
    [backView addSubview:self.topCollectionView];
    [self.topCollectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:TOPREUSE];
    
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if ([collectionView isEqual:self.collectionView]) {
        return 3;
    } else {
        return 1;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.collectionView]) {
        if (section == 0) {
            return 6;
        } else if (section == 1) {
            return 6;
        } else {
            return 6;
        }
    } else {
        return 12;
        }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if ([collectionView isEqual:self.collectionView]) {
        if (section == 0) {
            return CGSizeMake(0, 30);
        } else
            if (section == 1) {
                return CGSizeMake(0, 30);
            } else {
                return CGSizeMake(0, 30);
            }
    }
    else {
        return CGSizeMake(0, 0);
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.collectionView]) {
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FIRSTREUSE forIndexPath:indexPath];
        if (indexPath.section == 0) {
//            cell.backgroundColor = [UIColor yellowColor];
            return cell;
        } else if (indexPath.section == 1) {
//            cell.backgroundColor = [UIColor cyanColor];
            return cell;
        } else {
//            cell.backgroundColor = [UIColor greenColor];
            return cell;
        }
    } else {
        TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TOPREUSE forIndexPath:indexPath];
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            FirstCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:REUSEVIEW forIndexPath:indexPath];
            reuseView.backgroundColor = [UIColor whiteColor];
            return reuseView;
        } else if (indexPath.section == 1) {
                SecCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SECVIEW forIndexPath:indexPath];
                view.backgroundColor = [UIColor whiteColor];
                return view;
            } else {
                ThiredCollectionReusableView *thiredView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:THIREDVIEW forIndexPath:indexPath];
                thiredView.backgroundColor = [UIColor whiteColor];
                return thiredView;
            }
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
